//
//  MSFilterView.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/5.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFilterView.h"

#import "MSFilterSelectCell.h"

@interface MSFilterView()
<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *resetButton; // 重置按钮
@property (nonatomic, strong) UIButton *saveButton; // 保存按钮

@property (nonatomic, copy) NSArray *dataSource;
@property (nonatomic, copy) NSDictionary *selectData;
@property (nonatomic, strong) NSMutableDictionary *tempSelectData; // 选中的数据，用于切换选择但是未保存时起作用

@end

@implementation MSFilterView

- (instancetype)initWithFrame:(CGRect)frame dataSource:(NSArray *)dataSource selectData:(NSDictionary *)selectData {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        self.dataSource = dataSource;
        self.selectData = selectData;
        self.tempSelectData = selectData.mutableCopy;
        
        [self initSubViews];
    }
    return self;
}

#pragma mark - Init Method

- (void)initSubViews {
    CGFloat buttonWidth = MS_SCREEN_WIDTH / 2.0;
    CGFloat buttonHeight = 36.0;
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.offset(-buttonHeight);
    }];
    [self addSubview:self.resetButton];
    [self.resetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.width.offset(buttonWidth);
        make.top.equalTo(self.tableView.mas_bottom);
    }];
    [self addSubview:self.saveButton];
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(0);
        make.width.offset(buttonWidth);
        make.top.equalTo(self.tableView.mas_bottom);
    }];
//    [self performSelector:@selector(showButton) withObject:nil afterDelay:0.0];
//    [self showButton];
}

- (void)showButton {
    self.resetButton.hidden = NO;
    self.saveButton.hidden = NO;
}

- (void)hiddenButton {
    self.resetButton.hidden = YES;
    self.saveButton.hidden = YES;
    self.tempSelectData = self.selectData.mutableCopy;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MSFilterSelectCell *selectCell = [tableView dequeueReusableCellWithIdentifier:@"MSFilterSelectCell"];
    if (indexPath.row < self.dataSource.count) {
        NSDictionary *contentDic = self.dataSource[indexPath.row];
        NSString *idKey = [MSSafeObject stringInfo:contentDic[@"id"]];
        NSString *selectIdKey = [MSSafeObject stringInfo:self.tempSelectData[idKey]];
        [selectCell refreshFilterSelectCellWithDictionary:contentDic selectId:selectIdKey];
        _WEAKSELF
        selectCell.ShowButtonSelectBlock = ^(NSDictionary * _Nonnull selectFilterDic) {
            // 根据ID储存对应的选项ID
            [weakSelf.tempSelectData setObject:[NSString stringWithFormat:@"%@", selectFilterDic[@"id"]] forKey:idKey];
        };
    }
    
    return selectCell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < self.dataSource.count) {
        NSDictionary *contentDic = self.dataSource[indexPath.row];
        return [MSFilterSelectCell caculateFilterSelectCellHeightWithDictionary:contentDic];
    }
    return CGFLOAT_MIN;
}

#pragma mark - Event Response
/** 重置按钮点击 */
- (void)resetButtonClick:(UIButton *)sender {
    
    // 清空选中字典,刷新数据
    [self.tempSelectData removeAllObjects];
    self.selectData = self.tempSelectData.copy;
    [self.tableView reloadData];
    
    if (self.SaveButtonClickBlock) {
        self.SaveButtonClickBlock(0, nil);
    }
}
/** 保存按钮点击 */
- (void)saveButtonClick:(UIButton *)sender {
    // 赋值筛选控件
    self.selectData = self.tempSelectData.copy;

    if (self.SaveButtonClickBlock) {
        self.SaveButtonClickBlock(1, self.selectData);
    }
}

#pragma mark - Getters

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        
        [_tableView registerClass:[MSFilterSelectCell class] forCellReuseIdentifier:@"MSFilterSelectCell"];
    }
    return _tableView;
}

- (UIButton *)resetButton {
    if (!_resetButton) {
        _resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_resetButton setTitle:@"重置" forState:UIControlStateNormal];
        [_resetButton setTitleColor:[UIColor colorWithHexString:@"#4A4A4A"] forState:UIControlStateNormal];
        [_resetButton addTarget:self action:@selector(resetButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _resetButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _resetButton.layer.borderWidth = 0.5;
        _resetButton.layer.borderColor = rgba(0, 0, 0, 0.3).CGColor;
//        _resetButton.hidden = YES;
    }
    return _resetButton;
}

- (UIButton *)saveButton {
    if (!_saveButton) {
        _saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_saveButton setBackgroundColor:MSThemeColor];
        [_saveButton addTarget:self action:@selector(saveButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _saveButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        _saveButton.hidden = YES;
    }
    return _saveButton;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}
@end
